<?php
/**
 * Zead
 *
 * LICENSE	
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.opensource.org/licenses/bsd-license.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to eu@marcelomx.com so we can send you a copy immediately.
 *
 * @category   Zead
 * @author     Marcelo Rodrigues Gonzaga <eu@marcelomx.com>
 * @copyright  Copyright (c) 2008 Marcelo Rodrigues Gonzaga
 * @link       http://code.google.com/p/zead 
 * @license    http://www.opensource.org/licenses/bsd-license.php     New BSD License
 *
 **/


/** @see Zead_Html_Filter_Abstract **/
require_once 'Zead/Html/Filter/Abstract.php';

/**
 * @category   Zead
 * @package    Zead_Html
 * @subpackage Zead_Html_Filter
 * @copyright  Copyright (c) 2008 Marcelo Rodrigues Gonzaga 
 * @license    http://www.opensource.org/licenses/bsd-license.php     New BSD License  
 */
class Zead_Html_Filter_Paginator extends Zead_Html_Filter_Abstract
{
	/**
	 * Aplica o filtro que tem a função de procurar elementos com o id siteName ou outro
	 * passado como argumento que será o título do site.
	 * @param array $pagesRange
	 * @param string $currentPageUrl
	 * @param string $cssSelector
	 * @param string $scrollingStyle
	 */
	public function filter($pagesRange, $currentPageUrl = null, $selector = '#pageNav', $scrollingStyle = 'Sliding') 
	{
		if (!$pagesRange instanceof ArrayIterator) {
			throw(new Exception('The pagesRange is not instance of ArrayIterator'));
		}
		// If has pages
		if (!$pagesRange->count())
			return;
	
		// Retrieve paginator blocks
		$elements = $this->html->documentElement->select($selector);
		
		// If no has paginator blocks
		if (!$elements->count()) return;
		
		$currentPageNumber = $pagesRange->getPageNumber($currentPageUrl); // Current page
		$itemCountPerPage  = 1; // Limit the item per page
		
		// Creating the paginator
		
		/** @see Zend_Paginator **/
		require_once 'Zend/Paginator.php';		
		$paginator = Zend_Paginator::factory($pagesRange);
		$paginator->setItemCountPerPage($itemCountPerPage);
		$paginator->setCurrentPageNumber($currentPageNumber);	
		
		if (!$paginator->count())
			return;
			
		// Retrive the pages iterator
		$pages = $paginator->getPages();
		
		// Sliding style
		if ($scrollingStyle == 'Sliding') {
			// Creating previous page
			if ($pages->previous) {
				$page = $pagesRange->getPage($pages->previous);
				$code .= '<span class="previous"><a href="'. $page->url . '">Previous</a></span>';
			}
			
			// Writing the pages
			foreach ($pages->pagesInRange as $pageNumber) {
				$page = $pagesRange->getPage($pageNumber);
				$code .= ' <span ';
				if ($page->url == $currentPageUrl) {
					$code .= ' class="current"';
				}
				$code .= '><a href="'. $page->url . '">' . $page->label . '</a></span> ';
			}
			
			// Creating the next page link
			if ($pages->next) {
				$page = $pagesRange->getPage($pages->next);	
				$code .= '<span class="next"><a href="'. $page->url . '">Next</a></span>';
			}
			
		// Jumping style				
		} else if ($scrollingStyle == 'Jumping') {
			$code = '<select name="jumpinSelect" class="jumpingSelect">';
			// Creating options
			foreach ($pages->pagesInRange as $pageNumber) {
				$page = $pagesRange->getPage($pageNumber);
				$code .= '<option value="' . $page->url . '"';
				
				// Set title attribute
				if ($page->title != '') {
					$code .= ' title="'. $page->title . '"';
				}
				// Set the selected option
				if ($page->url == $currentPageUrl) {
					$code .= ' selected="selected"';	
				}
				$code .= '>'. $page->label . "</option>";
			}
			$code .= '</select>';
		}
		// Writing to html elements
		foreach ($elements as $elm) 
			$elm->update($code);
	}
}

